package cn.com.xinjinfu.pdoulcProduct.activity.user;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.adapter.PagerAdapter;
import cn.com.xinjinfu.pdoulcProduct.common.ConstantValues;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.FixedBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyVoucherBean;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.fragment.MyVoucher.CanUseListFragment;
import cn.com.xinjinfu.pdoulcProduct.fragment.MyVoucher.HaveUseListFragment;
import cn.com.xinjinfu.pdoulcProduct.fragment.MyVoucher.OverTimeListFragment;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;

/**
 * 我的兜券
 * Created by android-adt on 2015/12/31.
 */
public class MyVoucherListAct extends BaseActivity implements View.OnClickListener{

    private RequestQueue requen;
    private ViewPager mViewPager;
    private PagerAdapter mPagerAdapter;
    private TextView tv_can_use,tv_have_use,tv_finish;
    private LoadStatusBox loadbox;
    private MyVoucherBean myVoucher;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_voucher_list);
        //初始化头布局
        TitleManager.showTitle(this, "我的兜券", "");
        initView();
        loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
        loadbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netCheck();
            }
        });
        loadbox.loading();
        netCheck();
        requen = Volley.newRequestQueue(this);
        loginRequest();
    }

    private void netCheck(){
        if (!DataHandler.isNetworkConnected(this)) {
            loadbox.failed();
            return;
        }else {
            loadbox.success();
        }
    }
    private void initView() {
        tv_can_use = (TextView) findViewById(R.id.tv_can_use);
        tv_have_use = (TextView) findViewById(R.id.tv_have_use);
        tv_finish = (TextView) findViewById(R.id.tv_finish);
        tv_can_use.setOnClickListener(this);
        tv_have_use.setOnClickListener(this);
        tv_finish.setOnClickListener(this);
        mViewPager = (ViewPager)findViewById(R.id.view_pager);
        mPagerAdapter = new PagerAdapter(this);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                L.d("position=============++++" + position);
                if (position == 0) {
                    tv_can_use.setBackgroundColor(0xffF4B942);
                    tv_have_use.setBackgroundColor(0xffCCCCCC);
                    tv_finish.setBackgroundColor(0xffCCCCCC);
                } else if (position == 1) {
                    tv_can_use.setBackgroundColor(0xffCCCCCC);
                    tv_have_use.setBackgroundColor(0xffF4B942);
                    tv_finish.setBackgroundColor(0xffCCCCCC);
                } else {
                    tv_can_use.setBackgroundColor(0xffCCCCCC);
                    tv_have_use.setBackgroundColor(0xffCCCCCC);
                    tv_finish.setBackgroundColor(0xffF4B942);
                }
            }
        });
        /**
         * 此处可以用一个类，创建3个对象添加进来
         */
        mPagerAdapter.addTab(CanUseListFragment.class, null);
        mPagerAdapter.addTab(HaveUseListFragment.class, null);
        mPagerAdapter.addTab(OverTimeListFragment.class, null);
        mViewPager.setAdapter(mPagerAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId() ) {
            case R.id.tv_can_use:
                tv_can_use.setBackgroundColor(0xffF4B942);
                tv_have_use.setBackgroundColor(0xffCCCCCC);
                tv_finish.setBackgroundColor(0xffCCCCCC);
                mViewPager.setCurrentItem(0, true);
                break;
            case R.id.tv_have_use:
                tv_can_use.setBackgroundColor(0xffCCCCCC);
                tv_have_use.setBackgroundColor(0xffF4B942);
                tv_finish.setBackgroundColor(0xffCCCCCC);
                mViewPager.setCurrentItem(1, true);
                break;
            case R.id.tv_finish:
                tv_can_use.setBackgroundColor(0xffCCCCCC);
                tv_have_use.setBackgroundColor(0xffCCCCCC);
                tv_finish.setBackgroundColor(0xffF4B942);
                mViewPager.setCurrentItem(2, true);
                break;
        }
    }
    private void loginRequest() {
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
			final Map<String, String> parameters = DataHandler.getParameters("1426");
            parameters.put("id", user.getCurrUser().getSign());
			parameters.put("type", "0");
			parameters.put("currPage", "1");
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, null);
            request.setRetryPolicy(new DefaultRetryPolicy(ConstantValues.TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requen.add(request);
        }
    }

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.d("代金券jobj"+jobj.toString());
            myVoucher = JSON.parseObject(jobj.toString(), MyVoucherBean.class);
            tv_can_use.setText("可使用("+ myVoucher.getTotalRowsA() +")");
            tv_have_use.setText("已使用("+ myVoucher.getTotalRowsB() +")");
            tv_finish.setText("已过期("+ myVoucher.getTotalRowsC() +")");
        }
    };
}
